Circularr
Circular fixed size array
Install
npm i circularr
Usage
import Circularr from 'circularr'
const arrFrom = Circularr.from([1, 2, 3, 4, 5])
const arr = new Circular(3)
arr.fill(0)
arr.shift(16)
arr.shift(32)
console.log(...arr)
Api
class Circularr<T> {
constructor(length: number)
static from<T>(source: T[]): Circularr<T>
readonly length: number
[Symbol.iterator](): IterableIterator<T>
fill(value: T): this
shift(value: T): T
unshift(value: T): T
}
Fill
fill(value: T): this
Fills the array using value, effectively resetting it. Returns this
.
const circularr = new Circularr(3)
circularr.fill(0)
Shift
shift(value: T): T
shift
method pushes the value to the end of the array, wherein the first value gets popped out and returned.
const circularr = new Circularr(3).fill(0)
circularr.shift(8)
circularr.shift(16)
circularr.shift(32)
circularr.shift(64)
circularr.length
Unshift
unshift(value: T): T
unshift
does the opposite. It pushes the value to the front, popping the last value out.
const circularr = new Circularr(3).fill(0)
circularr.unshift(8)
circularr.unshift(16)
circularr.unshift(32)
circularr.unshift(64)
circularr.length
Iterable
Circular
implements iterable
protocol, so it can be used with any standard iterable syntax
const circularr = Circularr.from([1, 2, 3])
const [firstValue] = circularr
const copyToArray = [...circularr]
for (let value of circularr) {
console.log(value)
}